/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Unique String Collection Extension Data Class
 *		An extension of the StringCollection class to limit contents to unique values.
 *------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------------*/
using System;

namespace Adaptive.Foundation.Collections
{
    /// <summary>
    /// An extention of the <see cref="StringCollection"/> 
    /// class to provide extended functionality and represent a collection of strings.
    /// </summary>
    /// <remarks>
    /// <b>UniqueStringCollection</b> accepts a <b>null</b> reference (Nothing in Visual Basic) as a valid value but
    /// does not allow duplicate elements.
    /// 
    /// String comparisons are case-sensitive.
    /// 
    /// Indexes in this collection are zero-based.
    /// </remarks>
    [Serializable()]
    public sealed class UniqueStringCollection : StringCollection
    {
        /*--------------------------------------------------------------------------------------
         * Constructors
         *------------------------------------------------------------------------------------*/
        #region Constructors
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public UniqueStringCollection()
            : base()
        {
        }
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <param name="itemToParse">The string to be parsed into its parts.</param>
        /// <param name="delimiter">The delimiter to use in parsing.</param>
        public UniqueStringCollection(string itemToParse, string delimiter)
            : base(itemToParse, delimiter)
        {
        }
        /// <summary>
        /// Initializes an new instance of the class.
        /// </summary>
        /// <param name="capacity">The size of the collection.</param>
        public UniqueStringCollection(int capacity)
            : base(capacity)
        {
        }
        #endregion

        /*--------------------------------------------------------------------------------------
		 * Public Methods/Functions
		 *------------------------------------------------------------------------------------*/
        #region Public Methods/Functions
        /// <summary>
        /// Adds a new string to the collection if not already present.
        /// </summary>
        /// <param name="newValue">The string to be added.</param>
        /// <returns>
        /// The index of the string if inserted, or -1 if already present.
        /// </returns>
        public new int Add(string newValue)
        {
            int returnValue = -1;		//Return value.

            if (IndexOf(newValue) == -1)
                returnValue = base.Add(newValue);

            return returnValue;
        }
        /// <summary>
        /// Copies the elements of a <see cref="System.Collections.Specialized.StringCollection"/> array to the end of the 
        /// current <see cref="StringCollection"/>
        /// </summary>
        /// <param name="items">
        /// The <see cref="System.Collections.Specialized.StringCollection"/> object whose items are to be appended to 
        /// the current object.
        /// </param>
        public void AddRange(System.Collections.Specialized.StringCollection items)
        {
            foreach (string item in items)
            {
                if (IndexOf(item) == -1)
                    Add(item);
            }
        }
        #endregion
    }
}
